package cn.enilu.flash.api.controller;

import cn.enilu.flash.bean.entity.system.FileInfo;
import cn.enilu.flash.bean.enumeration.Permission;
import cn.enilu.flash.bean.vo.front.Rets;
import cn.enilu.flash.service.system.FileService;
import cn.enilu.flash.utils.CryptUtil;
import cn.enilu.flash.utils.HttpUtil;
import cn.enilu.flash.utils.Maps;
import cn.enilu.flash.utils.StringUtil;
import cn.enilu.flash.web.controller.BaseController;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.URLUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URISyntaxException;

@RestController
@RequestMapping("/file")
public class FileController extends BaseController {
    @Autowired
    private static final Logger logger = LoggerFactory.getLogger(FileController.class);
    @Autowired
    private FileService fileService;

    /**
     * 上传文件
     *
     * @param multipartFile
     * @return
     */
    @RequestMapping(method = RequestMethod.POST)
    @RequiresPermissions(value = {Permission.FILE_UPLOAD})
    public Object upload(@RequestPart("file") MultipartFile multipartFile) {

        try {
            FileInfo fileInfo = fileService.upload(multipartFile);
            return Rets.success(fileInfo);
        } catch (Exception e) {
            logger.error("上传文件异常", e);
            return Rets.failure("上传文件失败");
        }
    }

    /**
     * 下载文件
     *
     * @param fileName
     * @param fileName
     */
    @RequestMapping(value = "download", method = RequestMethod.GET)
    public void download(@RequestParam(value = "fileName", required = false) String fileName,
                         @RequestParam(value = "idFile", required = false) Long idFile) throws URISyntaxException {
        FileInfo fileInfo = null;
        if (StringUtil.isNotEmpty(fileName)) {
            fileInfo = fileService.getByName(fileName);
        }
        if (idFile != null) {
            fileInfo = fileService.get(idFile);
        }
        downloadFile(fileInfo);
    }

    public void downloadFile(FileInfo fileInfo) {
        String fileName = fileInfo.getOriginalFileName();
        HttpServletResponse response = HttpUtil.getResponse();
        response.setContentType("application/x-download");
        try {
            fileName = new String(fileName.getBytes(), "ISO-8859-1");
            response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
        } catch (Exception e) {
            e.printStackTrace();
        }
        try (OutputStream os = response.getOutputStream();) {
            os.write(cn.hutool.http.HttpUtil.createGet(fileInfo.getAblatePath()).execute().bodyBytes());
        } catch (Exception e) {
            logger.error("download error", e);
        }
    }

    /**
     * 获取base64图片数据
     *
     * @param fileName
     * @return
     */
    @RequestMapping(value = "getImgBase64", method = RequestMethod.GET)
    public Object getImgBase64(@RequestParam("idFile") String fileName) throws URISyntaxException {
        FileInfo fileInfo = fileService.getByName(fileName);
        String base64 = CryptUtil.encodeBASE64(cn.hutool.http.HttpUtil.createGet(fileInfo.getAblatePath()).execute().bodyBytes());
        return Rets.success(Maps.newHashMap("imgContent", base64));
    }

    /**
     * 获取图片流
     *
     * @param response
     * @param fileName
     */
    @RequestMapping(value = "getImgStream", method = RequestMethod.GET)
    public void getImgStream(HttpServletResponse response,
                             @RequestParam("idFile") String fileName) throws URISyntaxException {
        FileInfo fileInfo = fileService.getByName(fileName);
        FileInputStream fis = null;
        response.setContentType("image/" + fileInfo.getRealFileName().split("\\.")[1]);
        try (OutputStream out = response.getOutputStream();) {
            out.write(cn.hutool.http.HttpUtil.createGet(fileInfo.getAblatePath()).execute().bodyBytes());
            out.flush();
        } catch (Exception e) {
            logger.error("getImgStream error", e);
        }
    }
}
